package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQues;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesRepository;

/**
 * 错题本--学生端--修改错题原因
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesQuesCauseCrtController extends YxyApiBaseController {
    @Autowired
    private StuMistakesQuesRepository stuMistakesQuesRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistake/cause/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        String studentId = reqModel.studentId;
        String quesId = reqModel.quesId;
        List<String> mistakesReason = reqModel.mistakesReason;

        StuMistakesQues stuMistakesQues = stuMistakesQuesRepository.findByQuesIdAndStudentId(quesId, studentId);
        if (mistakesReason != null) {
            stuMistakesQues.setMistakesReason(mistakesReason);
        } else {
            stuMistakesQues.setMistakesReason(null);
        }

        stuMistakesQuesRepository.save(stuMistakesQues);
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 学生Id
        public String studentId;

        // 试题ID
        public String quesId;

        // 错误原因, 存储中文, 使用逗号分割
        public List<String> mistakesReason;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

    }
}
